{% load i18n adminmedia %}
<div class="inline-group tab-content" id="{{ inline_admin_formset.formset.prefix }}-group">
{% if inline_admin_formsett %}  <h2>{{ inline_admin_formset.opts.verbose_name_plural|title }}</h2> {% endif %}
{{ inline_admin_formset.formset.management_form }}
{{ inline_admin_formset.formset.non_form_errors }}
<table>
	<thead>
	{% for inline_admin_form in inline_admin_formset %}
	{% if forloop.first %}
		{% for fieldset in inline_admin_form %}
			{% for line in fieldset %}
				{% for field in line %}
					<td>{{ field.field.label }}</td>
				{% endfor %}
			{% endfor %}
		{% endfor %}
	{% endif %}
	{% endfor %}
	<td> Editar </td>
	<td> Excluir </td>
	</thead>
	<tbody>
	{% for inline_admin_form in inline_admin_formset %}
	{% if inline_admin_form.original %}
	<tr>
		{% for fieldset in inline_admin_form %}
			{% for line in fieldset %}
				{% for field in line %}
				<td>
				{% if field.field.name == "images" or field.field.name == "photo" %}
					<img width=100 heigth=100 src="{{ MEDIA_URL}}{{ field.field.value }}" />
				{% else %}
					{{ field.field.value }}
				{% endif %}
				</td>
				{% endfor %}
			{% endfor %}
		{% endfor %}
		<td> <a href="#{{ inline_admin_formset.formset.prefix }}-{{ forloop.counter0 }}" rel="facebox-images">Editar </a>
		<script type="text/javascript">
			django.jQuery(document).ready(function($) {
					django.jQuery('a[rel=facebox-images]').facebox({
						div:'#{{ inline_admin_formset.formset.prefix }}-{{ forloop.counter0 }}',
						loadingImage : '/media-real/img/loading.gif',   
						closeImage   : '/media-real/img/closelabel.png',
						});
			});
		</script>
		</td>
		<td> {% if inline_admin_formset.formset.can_delete and inline_admin_form.original %}<span class="delete">{{ inline_admin_form.deletion_field.field }} {{ inline_admin_form.deletion_field.label_tag }}</span>{% endif %} </td>
	</tr>
	{% endif %}

	{% endfor %}
	</tbody>
</table>
{% for inline_admin_form in inline_admin_formset %}<div class="hidden inline-related{% if forloop.last %} empty-form last-related{% endif %}" id="{{ inline_admin_formset.formset.prefix }}-{% if not forloop.last %}{{ forloop.counter0 }}{% else %}empty{% endif %}">
  <h3><b>{{ inline_admin_formset.opts.verbose_name|title }}:</b>&nbsp;<span class="inline_label">{% if inline_admin_form.original %}{{ inline_admin_form.original }}{% else %}#{{ forloop.counter }}{% endif %}</span>
  </h3>
  {% if inline_admin_form.form.non_field_errors %}{{ inline_admin_form.form.non_field_errors }}{% endif %}
  {% for fieldset in inline_admin_form %}
    {% include "admin/includes/fieldset.html" %}
  {% endfor %}
  {% if inline_admin_form.has_auto_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
  {{ inline_admin_form.fk_field.field }}
</div>{% endfor %}
</div>

<script type="text/javascript">
(function($) {
    $(document).ready(function() {
        var rows = "#{{ inline_admin_formset.formset.prefix }}-group .inline-related";
        var updateInlineLabel = function(row) {
            $(rows).find(".inline_label").each(function(i) {
                var count = i + 1;
                $(this).html($(this).html().replace(/(#\d+)/g, "#" + count));
            });
        }
        var reinitDateTimeShortCuts = function() {
            // Reinitialize the calendar and clock widgets by force, yuck.
            if (typeof DateTimeShortcuts != "undefined") {
                $(".datetimeshortcuts").remove();
                DateTimeShortcuts.init();
            }
        }
        var updateSelectFilter = function() {
            // If any SelectFilter widgets were added, instantiate a new instance.
            if (typeof SelectFilter != "undefined"){
                $(".selectfilter").each(function(index, value){
                  var namearr = value.name.split('-');
                  SelectFilter.init(value.id, namearr[namearr.length-1], false, "{% admin_media_prefix %}");
                })
                $(".selectfilterstacked").each(function(index, value){
                  var namearr = value.name.split('-');
                  SelectFilter.init(value.id, namearr[namearr.length-1], true, "{% admin_media_prefix %}");
                })
            }
        }
        var initPrepopulatedFields = function(row) {
            row.find('.prepopulated_field').each(function() {
                var field = $(this);
                var input = field.find('input, select, textarea');
                var dependency_list = input.data('dependency_list') || [];
                var dependencies = row.find(dependency_list.join(',')).find('input, select, textarea');
                if (dependencies.length) {
                    input.prepopulate(dependencies, input.attr('maxlength'));
                }
            });
        }
        $(rows).formset({
            prefix: "{{ inline_admin_formset.formset.prefix }}",
            addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
            formCssClass: "dynamic-{{ inline_admin_formset.formset.prefix }}",
            deleteCssClass: "inline-deletelink",
            deleteText: "{% trans "Remove" %}",
            emptyCssClass: "empty-form",
            removed: updateInlineLabel,
            added: (function(row) {
                initPrepopulatedFields(row);
                reinitDateTimeShortCuts();
                updateSelectFilter();
                updateInlineLabel(row);
            })
        });
    });
})(django.jQuery);
</script>
